import { createRouter, createWebHistory } from "vue-router";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/Login/index.vue"),
    },
    {
      path: "/layout",
      name: "layout",
      component: () => import("../views/Layout/index.vue"),
      children: [
        {
          path: "/layout/listing",
          name: "layout:listing",
          meta: {
            title: "房源管理",
            requireAuth: true,
            icon: "",
          },
          component: () => import("../views/Layout/Listing/index.vue"),
        },
        {
          path: "/layout/liveStream",
          name: "layout:liveStream",
          meta: {
            title: "直播管理",
            requireAuth: true,
            icon: "",
          },
          component: () => import("../views/Layout/LiveStream/index.vue"),
        },
        {
          path: "/layout/buy",
          name: "layout:buy",
          meta: {
            title: "团购管理",
            requireAuth: true,
            icon: "",
          },
          component: () => import("../views/Layout/Buy/index.vue"),
        },
        {
          path: "/layout/audit",
          name: "layout:audit",
          meta: {
            title: "审核管理",
            requireAuth: true,
            icon: "",
          },
          component: () => import("../views/Layout/Audit/index.vue")
        },
        {
          path: "/layout/orders",
          name: "layout:orders",
          meta: {
            title: "订单管理",
            requireAuth: true,
            icon: "",
          },
          component: () => import("../views/Layout/Orders/index.vue"),
        },
        {
          path: "/layout/consultation",
          name: "layout:consultation",
          meta: {
            title: "资讯管理",
            requireAuth: true,
            icon: "",
          },
          component: () => import("../views/Layout/Consultation/index.vue"),
        },
        {
          path: "/layout/broker",
          name: "layout:broker",
          meta: {
            title: "经纪人管理",
            requireAuth: true,
            icon: "",
          },
          component: () => import('../views/Layout/Broker/index.vue')
        }
        
      ]
    }
  ]
})
// router.beforeEach((to, from) => {
//   console.log(to)
//   // 而不是去检查每条路由记录
//   const token =localStorage.getItem('token')
//   console.log(token)
//   if (to.meta.requiresAuth && !token) {
//     return {
//       path: '/login',
//       // 保存我们所在的位置，以便以后再来
//       query: { redirect: to.fullPath },
//     }
//   }
// })
router.beforeEach(async (to, from) => {
  const token = localStorage.getItem("token");
  if (
    // 检查用户是否已登录
    to.meta.requireAuth &&
    !token
  ) {
    // 将用户重定向到登录页面
    return { name: "login", query: { redirect: to.fullPath } };
  }
});
export default router;
